package com.jinyu.jyly.ui.mine.sell;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.MallDaiBeiMallPageTabAdapter;
import com.jinyu.jyly.adapter.MineMallOrderTabAdapter;
import com.jinyu.jyly.base.BaseLazyFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.DaiBeiMallBean;
import com.jinyu.jyly.bean.MallListInfo;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 带被买商品订单
 * @author 62485
 */
public class MineSellMallTabFragment extends BaseLazyFragment<MineSellPresenter> implements MineSellContract.View, BaseQuickAdapter.OnItemChildClickListener{

    @BindView(R.id.rcv_recycleview)
    RecyclerView rcv_recycleview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.cl_empty)
    ConstraintLayout cl_empty;
    private boolean hasLoadMore = false;
    private boolean isFristLoad = true;
    private int PAGE_NO = 1;
    private int PAGE_SIZE = 10;
    private MineSellPresenter mPresenter;
    private int type;
    private MallDaiBeiMallPageTabAdapter mallDaiBeiMallPageTabAdapter;


    public static MineSellMallTabFragment newInstance(int type) {
        MineSellMallTabFragment newFragment = new MineSellMallTabFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }


    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_mine_buy_tab, container, false);
        return view;
    }

    @Override
    protected void loadData() {
        initInent();
        initRecycleview();
    }

    private void initInent() {
        Bundle arguments = getArguments();
        type = arguments.getInt("type");
        getOrderList();
    }

    private void getOrderList() {
        try {
            //获取待被买
            mTipDialog.show();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("page",PAGE_NO);
            String param = AES.encryptToHexString(jsonObject.toString());
            mPresenter.getPageMyGoodList(param);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initRecycleview() {
        rcv_recycleview.setNestedScrollingEnabled(false);
        rcv_recycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                PAGE_NO = 1;
                try {
                    //获取待被买
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("page",PAGE_NO);
                    String param = AES.encryptToHexString(jsonObject.toString());
                    mPresenter.getPageMyGoodList(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                hasLoadMore = false;
            }
        });

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                ++PAGE_NO;
                try {
                    //获取待被买
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("page",PAGE_NO);
                    String param = AES.encryptToHexString(jsonObject.toString());
                    mPresenter.getPageMyGoodList(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                hasLoadMore = true;
            }
        });
    }

    @Override
    public MineSellPresenter getPresenter() {
        if(mPresenter == null){
            mPresenter = new MineSellPresenter(getActivity(), this);
        }
        return mPresenter;
    }


    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.resetNoMoreData();
    }

    @Override
    public void onOrderList(AllInfoBean infoBean) {
        mTipDialog.dismiss();
    }

    /**
     * 空实现
     * @param infoBean
     */
    @Override
    public void onChangeOrder(AllInfoBean infoBean) {
        mTipDialog.dismiss();
    }

    /**
     * 我的商品列表
     * @param infoBean
     */
    @Override
    public void onMyGoodList(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.resetNoMoreData();
        switch (infoBean.getStatus()){
            case 200:
                try {
                    String decrypt = AES.decrypt(infoBean.getData());
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<MallListInfo>>() {}.getType();

                    List<MallListInfo> mList = gson.fromJson(decrypt, listType);
                    if(mList != null && mList.size() != 0){
                        cl_empty.setVisibility(View.GONE);
                        rcv_recycleview.setVisibility(View.VISIBLE);
                        MineMallOrderTabAdapter operationAdapter = new MineMallOrderTabAdapter(mList, R.layout.item_mine_mall_order_tab);
                        rcv_recycleview.setAdapter(operationAdapter);

                    }else{
                        cl_empty.setVisibility(View.VISIBLE);
                        rcv_recycleview.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }

    /**
     * 新接口（商品列表）
     * @param infoBean
     */
    @Override
    public void onPageMyGoodList(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.resetNoMoreData();
        switch (infoBean.getStatus()){
            case 200:
                try {
                    String decrypt = AES.decrypt(infoBean.getData());
                    Gson gson = new Gson();
                    DaiBeiMallBean daiBeiMallBean = gson.fromJson(decrypt, DaiBeiMallBean.class);
                    if(daiBeiMallBean != null){
                        int page = daiBeiMallBean.getPage();
                        if(isFristLoad){
                            List<DaiBeiMallBean.DataBean> dataList = daiBeiMallBean.getData();
                            mallDaiBeiMallPageTabAdapter = new MallDaiBeiMallPageTabAdapter(dataList, R.layout.item_mine_mall_order_tab);
                            rcv_recycleview.setAdapter(mallDaiBeiMallPageTabAdapter);
//                            mallDaiBeiMallPageTabAdapter.setOnItemClickListener(this);
                            isFristLoad = false;
                        }else{
                            if(!hasLoadMore){
                                //下拉加载
                                List<DaiBeiMallBean.DataBean> dataList = daiBeiMallBean.getData();
                                mallDaiBeiMallPageTabAdapter.replaceData(dataList);
                            }else{
                                //上拉加载
                                if (PAGE_NO <= page) {
                                    //说明有值
                                    List<DaiBeiMallBean.DataBean> dataList = daiBeiMallBean.getData();
                                    mallDaiBeiMallPageTabAdapter.addData(dataList);
                                }
                            }
                        }
                    }


                    List<DaiBeiMallBean.DataBean> data1List = mallDaiBeiMallPageTabAdapter.getData();
                    if(data1List != null && data1List.size() != 0){
                        cl_empty.setVisibility(View.GONE);
                        rcv_recycleview.setVisibility(View.VISIBLE);
                    }else{
                        cl_empty.setVisibility(View.VISIBLE);
                        rcv_recycleview.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }

    @Override
    public void onPageOrderList(AllInfoBean infoBean) {
        //空
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }



}
